/**
 * 
 */
package com.ms.service.module.baccarat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.service.module.baccarat.bin.BacrBasicUtil;
import com.ms.service.module.baccarat.bin.BacrCardIconType;
import com.ms.service.module.baccarat.bin.BacrCardInfo;
import com.xpec.c4.game.utility.GameUtility;

/**
 * 百家樂遊戲運作時使用的撲克牌資料結構
 * 
 * @author TW15010
 */
public class BacrCard {
	/** log4j object */
	private static final Log log = LogFactory.getLog(BacrCard.class);

	/**
	 * 撲克牌索引值 {@link BacrCardInfo#getIndex()}
	 */
	private int index;

	/**
	 * 卡片方向是否為反轉的
	 */
	private boolean reverse;

	/**
	 * 提供codec編碼的建構式
	 */
	public BacrCard(){
		
	}
	
	/**
	 * @param cardInfo
	 *            數據表中的卡片資訊
	 */
	public BacrCard(BacrCardInfo cardInfo) {
		// 卡片在數據表中的索引值
		index = cardInfo.getIndex();

		// 決定牌的方向是否為反向
		randomReverse();
	}

	@Override
	public String toString() {
		BacrCardInfo info = BacrBasicUtil.getCardInfo(index);
		return info.toString();
	}

	/**
	 * 隨機決定牌的方向是否為反向
	 */
	public void randomReverse() {
		if (GameUtility.randomInt(1, 2) == 1) {
			reverse = false;
		} else {
			reverse = true;
		}
	}

	/**
	 * 取得撲克牌詳細資訊
	 * 
	 * @return
	 */
	public BacrCardInfo getInfo() {
		BacrCardInfo info = BacrBasicUtil.getCardInfo(index);
		if (info == null) {
			if (log.isErrorEnabled()) {
				log.error(GameUtility.getCurMethodName() + ": card index " + index + " get info = null",
						new RuntimeException());
			}
		}
		return info;
	}

	/**
	 * 取得牌的點數
	 * 
	 * @return
	 */
	public int getValue() {
		BacrCardInfo info = getInfo();
		if (info == null) {
			return 0;
		}

		return info.getValue();
	}

	/**
	 * 是否為紅卡
	 * 
	 * @return
	 */
	public boolean isRedCard() {
		BacrCardInfo info = getInfo();
		if (info == null) {
			return false;
		}

		if (info.getIconType() == BacrCardIconType.RED_CARD) {
			return true;
		}

		return false;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public boolean isReverse() {
		return reverse;
	}

	public void setReverse(boolean reverse) {
		this.reverse = reverse;
	}
}
